package com.zl_huang.example.web;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zl_huang.example.model.Score;
import com.zl_huang.example.model.Subject;
import com.zl_huang.example.model.User;
import com.zl_huang.example.model.vo.ScoreVO;
import com.zl_huang.example.model.vo.SubjectVO;
import com.zl_huang.example.model.vo.TestVO;
import com.zl_huang.example.service.IScoreService;
import com.zl_huang.example.service.IStudentService;
import com.zl_huang.example.service.ISubjectService;
import com.zl_huang.example.service.ITestService;

@Controller
@RequestMapping(value = "/student")
public class StudentController {
	
	@Autowired
	private IStudentService studentService;
	@Autowired
	private ITestService  testService;
	@Autowired	
	private IScoreService scoreService;
	@Autowired 
	private ISubjectService subjectService;
	
	@RequestMapping(value = "/subjects")
	public String toSubject(HttpSession session,Model model){
		User user = (User)session.getAttribute("user");
		System.out.println(user.getUser_id()+"--"+user.getUser_name());
		List<SubjectVO> subjectList;
		try {
			subjectList = studentService.getSubjectList(user);
			model.addAttribute("subjectList", subjectList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "student/Student";
	}
	
	@ResponseBody
	@RequestMapping(value = "/showScore")
	public ScoreVO showScore(Integer scoreId){
		ScoreVO scoreVO = null;
		try {
			 scoreVO = studentService.getScoreById(scoreId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return scoreVO;
	}
	
	@RequestMapping(value = "/toExamTest/{sub_id}" , method = RequestMethod.GET)
	public String toExamTest(HttpSession session, @PathVariable(value = "sub_id") Integer sub_id){
	System.out.println("我进来了");
		User user = (User)session.getAttribute("user");
		System.out.println(user.getUser_id()+"--"+user.getUser_name()+"----"+sub_id);
		if(user != null && sub_id != 0){
			try {
				Map<Integer, List<TestVO>> resultTestListMap = testService.getTestListBySubject(sub_id, user.getUser_id());
				Score score = scoreService.getScoreByUserIdAndSubId(user.getUser_id(), sub_id);
				Subject subject = subjectService.getSubjectById(sub_id);
				session.setAttribute("subject", subject);
				long remainTime = 0;
				if(score != null){
					//计算剩余的考试时间
					Calendar startTime = Calendar.getInstance();
					startTime.setTime(score.getExam_time());
					Calendar currentTime = Calendar.getInstance();
					currentTime.setTime(new Date());
					long startSecond = startTime.getTimeInMillis();
					long currentSecond = currentTime.getTimeInMillis();	
					long totalSecond = subject.getTotal_time()*60;
					remainTime = totalSecond - (currentSecond-startSecond) / 1000;					
				}else{
					remainTime =  subject.getTotal_time()*60;
				}
				System.out.println("剩余考试时间为"+remainTime);
				session.setAttribute("remainTime", remainTime);
				
				List<TestVO> resultList = new ArrayList<TestVO>();
				List<String> answerList = new ArrayList<String>();
				if(!resultTestListMap.isEmpty()){
					for(Integer key : resultTestListMap.keySet()){
						resultList.addAll(resultTestListMap.get(key));
						for(TestVO testVO : resultTestListMap.get(key)){
							answerList.add(testVO.getAnswer());
							System.out.println("这是集合答案"+testVO.getAnswer());
						}
					}
					session.setAttribute("answerList", answerList);
					session.setAttribute("resultList", resultList);
				}
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "student/ExamTest";
	}
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "/checkAnswer")
	public boolean checkAnswer(HttpSession session, Integer subId ,String[]  result){
		User user = (User)session.getAttribute("user");
		List<String> list =(List<String>) session.getAttribute("answerList");
		boolean flag = false;
		if((!list.isEmpty()) && result.length > 0 && list.size() == result.length ){
			double totalScore = 0;			
			for(int i = 0 ; i < result.length ; i++){
				if(i < (result.length/2)){
					if(result[i].trim().equals(list.get(i).trim())){
						totalScore += 10;
					}
					
				}else{
					if(result[i].trim().equals(list.get(i).trim())){
						totalScore += 15;
					}
					
				}
			}
			try {
				Score score = scoreService.getScoreByUserIdAndSubId(user.getUser_id(), subId);
				if(score != null){
					score.setScore(totalScore);
					flag = scoreService.editScore(score);
					session.removeAttribute("answerList");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		return flag;
		
	}

}
